
// import haxe.ds.Vector;
// import oimo.common.Setting;
// import oimo.common.Vec3;

import { Vec3 } from "../../common/Vec3";
import { Setting } from "../../common/Setting";
import { DetectorResultPoint } from "./DetectorResultPoint";

/**
 * The result of narrow-phase collision detection. This is used for generating contact
 * points of a contact constraint at once or incrementally.
 */

export class DetectorResult {
	/**
	 * The number of the result points.
	 */
	numPoints: number;

	/**
	 * The result points. Note that **only the first `DetectorResult.numPoints` points are
	 * computed by the collision detector**.
	 */
	points: Array<DetectorResultPoint>;

	/**
	 * The normal vector of the contact plane.
	 */
	normal: Vec3;

	/**
	 * Whether the result points are to be used for incremental menifold update.
	 */
	incremental: boolean; // for GJK/EPA detector

	/**
	 * Default constructor.
	 */
	constructor() {
		this.numPoints = 0;
		this.normal = new Vec3();
		this.points = new Array<DetectorResultPoint>(Setting.maxManifoldPoints);
		this.incremental = false;

		for (let i = 0; i < Setting.maxManifoldPoints; ++i) {
			this.points[i] = new DetectorResultPoint();
		}
	}

	// --- public ---

	/**
	 * Returns the maximum depth of the result points. Returns `0.0` if no result
	 * points are available.
	 */
	public getMaxDepth(): number {
		var max: number = 0;
		for (let i = 0; i < this.numPoints; ++i) {
			if (this.points[i].depth > max) {
				max = this.points[i].depth;
			}
		}
		return max;
	}

	/**
	 * Cleans up the result data.
	 */
	public clear(): void {
		for (let p of this.points) {
			p.position1.zero();
			p.position2.zero();
			p.depth = 0;
			p.id = 0;
		}
		this.numPoints = 0;
		this.normal.zero();
	}
}
